//
//  CarPicViewController.m
//  MansApp
//
//  Created by lanou3g on 15/7/17.
//  Copyright (c) 2015年 ACH. All rights reserved.
//

#import "CarPicViewController.h"
#import "ACHFlowLayout.h"
#import "CarPicCollectionViewCell.h"
@interface CarPicViewController ()<iCarouselDataSource,iCarouselDelegate>//<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property(nonatomic,strong)UIPickerView *pickerView;
@property(nonatomic,strong)iCarousel *icarousel;
@property(nonatomic,strong)UICollectionView *collection;
@end

@implementation CarPicViewController

- (void)viewDidLoad {
    [super viewDidLoad];

//    ACHFlowLayout *layout = [[ACHFlowLayout alloc]init];
//    
//    self.collection  = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight - kP6Height(50)) collectionViewLayout:layout];
//    
//    self.collection.delegate = self;
//    self.collection.dataSource = self;
//    [self.view addSubview: self.collection];
   
    UIImageView *backgroundView = [[UIImageView alloc]initWithFrame:self.view.bounds];
    backgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    backgroundView.image = [UIImage imageNamed:@"background123"];
    [self.view addSubview:backgroundView];
    self.view.backgroundColor = [UIColor clearColor];
    self.icarousel = [[iCarousel alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight - kP6Height(50))];
    self.icarousel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    self.icarousel.type = iCarouselTypeRotary;
    self.icarousel.delegate =self;
    self.icarousel.dataSource = self;
//    self.icarousel.vertical = YES;
    self.icarousel.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_icarousel];

    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(0, kHeight - kP6Height(50),kWidth, kP6Height(50));
    [button setTitle:@"back" forState:UIControlStateNormal];
    button.backgroundColor = [UIColor colorWithRed:128 / 255.0 green:128 / 255.0  blue:0 / 255.0 alpha:0.8];
    [button addTarget:self action:@selector(buttonAciton) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview: button];
    
}


- (NSInteger)numberOfItemsInCarousel:(iCarousel *)carousel{
    
    return self.picArray.count;
}

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSInteger)index reusingView:(UIView *)view{
    
    if (view == nil) {
        view = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kP6width(300), kP6Height(200))];
        
       view.contentMode = UIViewContentModeScaleAspectFill;
        view.center = carousel.center;
        ((UIImageView *)view).image = [UIImage imageNamed:@"page"];

    }
    if ([self.picArray[index] isKindOfClass:[UIImage class]]) {
        ((UIImageView *)view).image = self.picArray[index];
    }
    else
    {
    [(UIImageView *)view sd_setImageWithURL:[NSURL URLWithString:self.picArray[index]] placeholderImage:[UIImage imageNamed:@"page"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
//        CGSize size = image.size;
//        CGFloat scale = size.width / 300;
//        view.frame = CGRectMake(0, 0, 300, size.height / scale);
//        view.center = carousel.center;
    }];}
    
    return view;
}

//- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
//    
//    return self.picArray.count;
//}
//
//- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
//    
//    static NSString *cellIdentifier = @"cell";
//    [collectionView registerClass:[CarPicCollectionViewCell class] forCellWithReuseIdentifier:cellIdentifier];
//    CarPicCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
//    if ([[self.picArray[indexPath.row] class]isSubclassOfClass:[NSString class]]) {
//        
//   
//    
//    [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:self.picArray[indexPath.row]] placeholderImage:[UIImage imageNamed:@"8-1"]completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
//        [self.picArray setObject:image atIndexedSubscript:indexPath.row];
//    }];
//     }
//    else{
//        
//        cell.picImageView.image = self.picArray[indexPath.row];
//    }
//    
//    
//    return cell;
//    
//}
//- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
//    
//
//    
//    return UIEdgeInsetsMake(kHeight / 2 - kP6Height(100), 50, kHeight / 2 - kP6Height(100), 50);
//}
//
//
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
//    
//    
//    CGSize size = [self whatSizeIsMy:indexPath];
//    
//    CGFloat scale = size.width / 150;
//    
//    
//   return CGSizeMake(150,size.height / scale);
//
//}
//
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
//    
//    
//    return 150;
//}
//
//
//- (CGSize)whatSizeIsMy:(NSIndexPath*)indexPath{
//    
//    
//    CGSize size ;
//    
//    if ([[self.picArray[indexPath.row] class]isSubclassOfClass:[NSString class]]) {
//        UIImageView *view = [[UIImageView alloc]init];
//        __block CGSize myS = size;
//        [view sd_setImageWithURL:[NSURL URLWithString:self.picArray[indexPath.row]] placeholderImage:[UIImage imageNamed:@"8-1"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
//            myS = image.size;
//            
//            [self.picArray setObject:image atIndexedSubscript:indexPath.row];
//        }];
//        
//    }
//    else{
//        size = [self.picArray[indexPath.row] size];
//    }
//    
//    return size;
//    
//}
- (void)buttonAciton{
    
    
    [self  dismissViewControllerAnimated:NO completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
